﻿using BaoVeBienCuong.Playgame.enemy;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;

namespace BaoVeBienCuong.Playgame.army
{
    public class LinhTamXa : LinhCanChien
    {
        Dan dan;
        public LinhTamXa(string loai, int level)
        {
            setLoai(loai, level);
            dan = new Dan((loai.Equals("archer") ? level : level + 3), 45, position, 0);
        }

        public override void Load(ContentManager cont)
        {
            dan.Load(cont);
            imageTam = cont.Load<Texture2D>("images/towers/HongTam");
            foreach (NangCap nangcap in nangcaps)
            {
                nangcap.Load(cont);
            }
            imagesao = cont.Load<Texture2D>("images/towers/star");
            imageLock = cont.Load<Texture2D>("images/store/lock");
            if (Loai.Equals("archer"))
            {
                LoadArcher(cont);
            }
            else
            {
                LoadShaman(cont);
            }
        }
        public void LoadArcher(ContentManager cont)
        {
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("images/towers/archer_" + level + "1");
            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("images/towers/archer_" + level + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("images/towers/archer_" + level + "2");
            }

            if (level == 0)
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(495, 153);
                    if (huong.Equals("TopRight"))
                    {
                        Lechposition = new Vector2(20, -70) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(10, -70) * scale;
                    }
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(572, 154);
                    Lechposition = new Vector2(0, -70) * scale;
                }
                else
                {
                    sizeImages = new Vector2(605, 145);
                    if (huong.Equals("Left"))
                    {
                        Lechposition = new Vector2(10, -60) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-10, -60) * scale;
                    }
                }
                #endregion
                //if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(495, 153);
                //else if (huong.Equals("Bottom")) sizeImages = new Vector2(572, 154);
                //else sizeImages = new Vector2(605, 145);
            }
            else if (level == 1)
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(690, 198);
                    if (huong.Equals("TopRight"))
                    {
                        Lechposition = new Vector2(10, -105) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-20, -105) * scale;
                    }
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(752, 145);
                    Lechposition = new Vector2(-30, -60) * scale;
                }
                else
                {
                    sizeImages = new Vector2(775, 167);
                    if (huong.Equals("Left"))
                    {
                        Lechposition = new Vector2(0, -60) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-30, -50) * scale;
                    }
                }
                #endregion
                //if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(690, 198);
                //else if (huong.Equals("Bottom")) sizeImages = new Vector2(752, 145);
                //else sizeImages = new Vector2(775, 167);
            }
            else
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(800, 211);
                    if (huong.Equals("TopRight"))
                    {
                        Lechposition = new Vector2(0, -105) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-30, -105) * scale;
                    }
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(876, 158);
                    Lechposition = new Vector2(-40, -80) * scale;
                }
                else
                {
                    sizeImages = new Vector2(810, 177);
                    if (huong.Equals("Left"))
                    {
                        Lechposition = new Vector2(0, -80) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-20, -70) * scale;
                    }
                }
                #endregion
                //if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(800, 211);
                //else if (huong.Equals("Bottom")) sizeImages = new Vector2(876, 158);
                //else sizeImages = new Vector2(810, 177);
            }
        }
        public void LoadShaman(ContentManager cont)
        {
            if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
            {
                image = cont.Load<Texture2D>("images/towers/shaman_" + level + "1");
            }
            else if (huong.Equals("Bottom"))
            {
                image = cont.Load<Texture2D>("images/towers/shaman_" + level + "0");
            }
            else
            {
                image = cont.Load<Texture2D>("images/towers/shaman_" + level + "2");
            }

            if (level == 0)
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(950, 214);
                    if (huong.Equals("TopRight"))
                    {
                        Lechposition = new Vector2(-30, -80) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-30, -80) * scale;
                    }
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(660, 170);
                    Lechposition = new Vector2(-20, -70) * scale;
                }
                else
                {
                    sizeImages = new Vector2(955, 185);
                    if (huong.Equals("Left"))
                    {
                        Lechposition = new Vector2(-20, -70) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-40, -70) * scale;
                    }
                }
                #endregion
                //if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(950, 214);
                //else if (huong.Equals("Bottom")) sizeImages = new Vector2(660, 170);
                //else sizeImages = new Vector2(955, 185);
            }
            else if (level == 1)
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(1460, 217);
                    if (huong.Equals("TopRight"))
                    {
                        Lechposition = new Vector2(-120, -140) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-40, -140) * scale;
                    }
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(824, 201);
                    Lechposition = new Vector2(-80, -120) * scale;
                }
                else
                {
                    sizeImages = new Vector2(1345, 221);
                    if (huong.Equals("Left"))
                    {
                        Lechposition = new Vector2(-100, -140) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-30, -140) * scale;
                    }
                }
                #endregion
                //if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(1460, 217);
                //else if (huong.Equals("Bottom")) sizeImages = new Vector2(824, 201);
                //else sizeImages = new Vector2(1345, 221);
            }
            else
            {
                #region
                if (huong.Equals("TopLeft") || huong.Equals("TopRight"))
                {
                    sizeImages = new Vector2(1460, 234);
                    if (huong.Equals("TopRight"))
                    {
                        Lechposition = new Vector2(-110, -150) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-50, -150) * scale;
                    }
                }
                else if (huong.Equals("Bottom"))
                {
                    sizeImages = new Vector2(1012, 224);
                    Lechposition = new Vector2(-120, -140) * scale;
                }
                else
                {
                    sizeImages = new Vector2(1480, 242);
                    if (huong.Equals("Left"))
                    {
                        Lechposition = new Vector2(-100, -160) * scale;
                    }
                    else
                    {
                        Lechposition = new Vector2(-60, -150) * scale;
                    }
                }
                #endregion
                //if (huong.Equals("TopLeft") || huong.Equals("TopRight")) sizeImages = new Vector2(1460, 234);
                //else if (huong.Equals("Bottom")) sizeImages = new Vector2(1012, 224);
                //else sizeImages = new Vector2(1480, 242);
            }
        }

        public override void Ban(List<Enemy> enemy)
        {
            bool dangban = true;
            Random rd = new Random();
            int j;
            if (isVisible)
            {
                if (dan.getEnable())
                {
                    dangban = true;
                    dan.Dichuyen(enemy);
                }
                else
                {
                    for (j = 0; j < enemy.Count; j++)
                    {
                        if (GamePage.save.GetValue(Loai, level, sosao).getTamXa() * scale >= (float)KhoangCach(enemy[j]) && enemy[j].getEnable())
                        {
                            Quay(enemy[j]);
                            if (count % ((int)(40 / GamePage.save.GetValue(Loai, level, sosao).getTocdo())) == 4)
                            {
                                dan.setSatthuong(GamePage.save.GetValue(Loai, level, sosao).getSatthuong(),
                                    (rd.Next(1000) % 100 <= GamePage.save.GetValue(Loai, level, sosao).getTiLe()) ? GamePage.save.GetValue(Loai, level, sosao).getHieuUng() : "");
                                dan.setDuongdi(getTam() + new Vector2(10, 10), enemy[j].getTam(), j);
                                if (Loai.Equals("archer")) MainPage.PlayHieuUng("one_handed", false);
                                else
                                    MainPage.PlayHieuUng("shaman_0", false);
                            }
                            break;
                        }
                    }
                    if (j == enemy.Count) dangban = false;
                    else dangban = true;
                }
            }
            if (dangban == false)
            {
                huong = "Bottom";
            }
        }

        public override void Draw(SpriteBatch sprite, SpriteFont font, bool visibleStar = true)
        {
            if (dan.getEnable())
            {
                dan.Draw(sprite);
            }
            base.Draw(sprite, font, visibleStar);

        }
        public override bool TouchNangCap(Vector2 posi)
        {
            if (Vector2.Distance(posi, position * scale + GamePage.DoLech) > 300 * GamePage.scale) // nếu chạm ngoài vùng phủ sóng thì dừng nâng cấp
            {
                return false;
            }
            else
            {
                foreach (NangCap nangcap in nangcaps)
                {
                    if (nangcap.isTouch(posi))
                    {
                        int k = nangcap.getType();
                        if (k == 3)
                        {
                            GamePage.money += nangcap.getPrice();
                            isVisible = false;
                            return false;
                        }
                        else if (k == 2)
                        {
                            sosao++;
                            GamePage.money -= nangcap.getPrice();
                        }
                        else if (k == 1)
                        {
                            GamePage.money -= nangcap.getPrice();
                            level = 1;
                            sosao = 1;
                            dan = new Dan((Loai.Equals("archer") ? level : level + 3), 45, position, 0);
                        }
                        else if (k == 0)
                        {
                            GamePage.money -= nangcap.getPrice();
                            level = 2;
                            sosao = 1;
                            dan = new Dan((Loai.Equals("archer") ? level : level + 3), 45, position, 0);
                        }
                    }
                }
                return true;
            }
        }
    }
}
